import QtQuick 1.0

Image {
    id:mainScreen
    source: "images/main_screen/background3.png"
    anchors.fill: parent
    property int gameState: 1// 1: normal, 2: eating, 3: walking 4: fighting
    Text {
        id: debugText
        text: "debugText"
        font.family: "Helvetica"
        font.pointSize: 24
        color: "red"
        x: 320
        y: 10
    }
    Image {
        id: navBar
        source:  "images/main_screen/nav.png"
        opacity:  0.5
        width:  155
        height: 60
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.topMargin: 20
    }

    Row {
        id: navMenu
        anchors.left: navBar.left
        anchors.top: navBar.top
        anchors.topMargin: 10
        spacing: 14
        Button {
            id: tapvo
            width: 39
            height: 39
            anchors.verticalCenter: parent.verticalCenter
            normalImgPath: "images/main_screen/tap-vo.png"
            pressedImgPath: "images/main_screen/tap-vo-click.png"
            onClicked: {
                //do something
                debugText.text = "Tap vo"
                gameState = 4// fighting
                 mainScreen.source = "images/tapvo.png"
                chick.fight()
            }
        }

        Button {
            id: didao
            width: 39
            height: 39
            anchors.verticalCenter: parent.verticalCenter
            normalImgPath: "images/main_screen/di-dao.png"
            pressedImgPath: "images/main_screen/di-dao-click.png"
            onClicked: {
                //do something
                debugText.text = "di dao"
                gameState = 3 // walking
                mainScreen.source = "images/didao.png"
                chick.fight()
            }
        }

        Button {
            id: choan
            width: 39
            height: 39
            anchors.verticalCenter: parent.verticalCenter
            normalImgPath: "images/main_screen/cho-an.png"
            pressedImgPath: "images/main_screen/cho-an-click.png"
            onClicked: {
                //do something
                debugText.text = "cho an"
                gameState = 2 // eating
                mainScreen.source = "images/main_screen/background3.png"
                chick.eat()
            }
        }
/*
        Button {
            id: don
            width: 39
            height: 39
            anchors.verticalCenter: parent.verticalCenter
            normalImgPath: "images/main_screen/don.png"
            pressedImgPath: "images/main_screen/don-click.png"
            onClicked: {
                //do something
                mainScreen.source = "images/cage/background.png"
            }
        }*/
    }
    Button {
        id: fight
        width:  155
        height: 60
        anchors.left: parent.left
        anchors.bottom: parent.top
        anchors.bottomMargin: 20
        normalImgPath: "images/main_screen/bluetooth.png"
        pressedImgPath: "images/main_screen/bluetooth-click.png"
        onClicked: {
            // find device on bluetooth and connect

        }
    }

    Timer{
        id:createShitTimer
        interval: 10000// 10 seconds
        running:true
        repeat:true
        onTriggered:{
            if(gameState == 1)// normal
            {
                createShit();
            }
        }
    }
    function createShit()
    {
        // doc du lieu
        // duyet qua 10
        // add 10 cai vo
        Qt.createQmlObject("import QtQuick 1.0\nShit {\n    id: smile\n    x:100 + 440*Math.random()\n    y: 200 + 220*Math.random() \n z:2}"
                    ,mainScreen);
        // shit under fowl = > z:2
    }
    Timer{
        id:changeActionTimer
        interval: 60

        running:true
        repeat:true
        onTriggered:{
            console.log(chick.framesCount);
            if(chick.currentFrame == 9 /*&& gameState == 2*/)// end frame of eating
            {
                console.log("eat done")
                chick.moveLeft()
                gameState = 1
                chick.currentFrame = 0
            }
            if(gameState != 1)// != normal
                return
            if(chick.x >= 400)
            {
                if(chick.y >= 250)
                {
                    chick.moveBack()
                }
                else if (chick.y <= 150){
                    chick.moveLeft()
                }
            }
            else if (chick.x <= 100)
            {
                if(chick.y <= 150)
                {
                    chick.moveFront()
                }
                else if (chick.y >= 250){
                    chick.moveRight()
                }
            }
        }
    }

    Sprite {
        id: chick
        x: 100
        y: 250
        z: 3 // fowl on the top

        /*
        property int framesCount:4
        property int currentFrame:0
        property string sourceImg:"images/main_screen/sprite-ga-di-phai.png"
        property int intervalTime:  300
        property int intervalTimeMove: 200
        property int speedX: 5
        property int speedY: 1
        */

    }


}
